package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ExamResult;
import com.think.teaching.service.impl.ExamResultServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生学生考试答案 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "考试结果管理接口")
public class ExamResultController {
	
	private final ExamResultServiceImpl examResultService;

	public ExamResultController(ExamResultServiceImpl examResultService) {
		this.examResultService = examResultService;
	}

	@ApiOperation("添加新的学生考试答案")
	@PostMapping("admin/examResult")
	public Result AddNewExamResult(@RequestBody()ExamResult data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ExamResult examResult = examResultService.AddExamResult(data);
			if (examResult != null) {
				return Result.success(examResult);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新学生考试答案")
	@RequestMapping(value = "admin/examResult",method = RequestMethod.PUT)
	public Result UpdateExamResult(@RequestBody()ExamResult data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ExamResult examResult = examResultService.UpdateExamResult(data);
			if (examResult != null) {
				return Result.success(examResult);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除学生考试答案")
	@RequestMapping(value = "admin/examResult/{id}",method = RequestMethod.DELETE)
	public Result DeleteExamResult(@PathVariable("id")Integer id) {
		ExamResult examResult = examResultService.getById(id);
		if (examResult == null) {
			return Result.failure(ResultCode.EXAM_RESULT_NOT_EXIST);
		} else {
			if (examResultService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取学生考试参案详情")
	@RequestMapping(value = "admin/examResult/{id}",method = RequestMethod.GET)
	public Result GetExamResult(@PathVariable("id")Integer id) {
		ExamResult examResult = examResultService.getById(id);
		if (examResult == null) {
			return Result.failure(ResultCode.EXAM_RESULT_NOT_EXIST);
		} else {

			return Result.success(examResult);
		}
	}
	
	@ApiOperation("管理员修改学生考试答案状态")
	@RequestMapping(value = "admin/examResult/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateExamResultState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		ExamResult examResult = examResultService.getById(id);
		if (examResult == null) {
			return Result.failure(ResultCode.EXAM_RESULT_NOT_EXIST);
		} else {
			try {
				examResult = examResultService.SetExamResultState(id, state);
				return Result.success(examResult);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索学生考试答案")
	@RequestMapping(value = "user/examResult/search",method = RequestMethod.GET)
	public Result UserSearchExamResult(@RequestParam("key")String key) {
		return Result.success(examResultService.SearchExamResultWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索学生考试答案")
	@RequestMapping(value = "admin/examResult/search",method = RequestMethod.GET)
	public Result AdminSearchExamResult(@RequestParam("key")String key) {
		return Result.success(examResultService.SearchExamResultWithKey(key, false));
	}
	
	@ApiOperation("管理员搜索学生考试答案列表")
	@RequestMapping(value = "admin/examResults/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListExamResult(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam("tag") Integer tag, Integer type) {
		return Result.success(examResultService.ListExamResultWithTagAndType(count, page, type, tag, false));
	}
	
	@ApiOperation("用户搜索学生考试答案列表")
	@RequestMapping(value = "user/examResults/{page}/{count}",method = RequestMethod.GET)
	public Result UserListExamResult(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam("tag") Integer tag, Integer type) {
		return Result.success(examResultService.ListExamResultWithTagAndType(count, page, type, tag, true));
	}
}

